
/* Generated by Interface Builder */

#import "Controller.h"
#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Pasteboard.h>
#import <appkit/View.h>
#import <appkit/Window.h>
#import <appkit/TextField.h>
#import <appkit/ScrollView.h>
#import <appkit/Scroller.h>
#import <appkit/Form.h>
#import <appkit/Text.h>
#import <streams/streams.h>
#import <strings.h>

@implementation Controller

- appDidInit:sender
{
    [[NXApp appListener] setServicesDelegate:self];
    return self;    
}

//this probably should be broken up into two funcs.  One for an RTF message and one for
//an ASCII message.  Oh well, it works...

- servicesMessage:(id)pasteboard
    userData:(const char *)sortArgs
    error:(char **)errorMessage
{
   int length, currentType;
    const char *data;
    const char *const *ptypes;
	NXStream	*stream;
    
       
    ptypes=[pasteboard types];
    
   //Look for RTF first...
   	for (currentType=0; ptypes[currentType] ; currentType++ ){
		if (!strcmp(ptypes[currentType],NXRTFPboardType)){
			[pasteboard readType:ptypes[currentType] data:&data length:&length];
			stream = NXOpenMemory(data, length, NX_READONLY);
			[[contentField docView] readRichText:stream];
			NXCloseMemory(stream, NX_FREEBUFFER);
			return self;
		
		}
	}
   	for (currentType=0; ptypes[currentType] ; currentType++ ){
		if (!strcmp(ptypes[currentType],NXAsciiPboardType))  {
			[pasteboard readType:ptypes[currentType] data:&data length:&length];
			stream = NXOpenMemory(data, length, NX_READONLY);
			[[contentField docView] readText:stream];
			NXCloseMemory(stream, NX_FREEBUFFER);
			return self;
			
		}
	 }
	return self;	//should never be reached....
}

- setOutputTitleField:anObject

{
	outputTitleField = anObject;
	[outputTitleField setDrawRotation:(NXCoord)90.0];
	[[outputTitleField setVertScrollerRequired:NO] display];
	
	return self;
}

- setContentField:anObject

{
	contentField = anObject;
	[[contentField setVertScrollerRequired:NO] display];
	return self;
}

- doPrint:sender

{
	[group printPSCode:self];
    return self;
}

- copyText:sender

{	NXStream	*stream;
	char			*buffer;
	int			length, maxlength;
	
	stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	[[inputTitleField docView] writeRichText:stream];
	NXGetMemoryBuffer(stream, &buffer, &length, &maxlength);
	NXCloseMemory(stream, NX_SAVEBUFFER);

	stream = NXOpenMemory(buffer, length, NX_READONLY);
	[[outputTitleField docView] readRichText:stream];
	NXCloseMemory(stream, NX_FREEBUFFER);


    return self;
}

- doScale:sender

{	NXCoord	newScale;
	id cView;
	NXRect	rect;
	
	cView = [window contentView];
	newScale = [scaleForm floatValueAt:0];
	[cView getFrame:&rect];
	[window sizeWindow :rect.size.width*newScale :rect.size.height*newScale];
	[cView scale:newScale :newScale];
	[window display];
	return self;
}

@end